home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / BASE_STA.C < prev    next >
C/C++ Source or Header  |  1992-09-23  |  8KB  |  216 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/29/89 -- Initial design and implementation
  13. // Updated: MJF 03/12/90 -- Added group names to RAISE
  14. // Updated: JAM 09/23/92 -- added def for alloc_size_s and initialized
  15. //
  16. // The CoolStack class is  publicly  derived from the  Generic class and is used to
  17. // implement non-type specific functionality for the parameterized CoolStack class.
  18. // In  this manner, code common  to  all instances  of the  CoolStack  class can be
  19. // shared to reduce code replication.  The  CoolStack class  implements a one
  20. // dimensional vector of a user-specified type.  This  is accomplished by using
  21. // the parameterized type  capability of C++.  The  stack will grow dynamically
  22. // as necessary  with   the amount  of  growth determined  by  the value  of an
  23. // allocation size slot.  Fixed length stacks are also supported by setting the
  24. // value of the allocation size slot to zero.
  25. //
  26.  
  27. #ifndef BASE_STACKH                // If no definition for CoolStack
  28. #include <cool/Base_Stack.h>            // Include definition file
  29. #endif
  30.  
  31. // int alloc_size_s -- Allocation size for growth
  32. int CoolBase_Stack::alloc_size_s = STACK_MEM_BLK_SZ;
  33.  
  34. // long set_length(long, char*) -- Change number of elements in this stack
  35. // Input:                          Integer number of elements and
  36. //                                 character string of type
  37. // Output:                         Number of elements
  38.  
  39. long CoolBase_Stack::set_length (long n, const char* Type) {
  40. #if ERROR_CHECKING
  41.   if (n < 0) {                    // If index out of range
  42.     //RAISE (Error, SYM(CoolStack), SYM(Negative_Length),
  43.     printf ("CoolStack<%s>::set_length(): Negative length %d.\n", Type, n);
  44.     abort ();
  45.   }
  46. #endif
  47.   if (n <= size)                // If not greater than size
  48.     this->number_elements = n;            // Set new length
  49.   else
  50.     this->number_elements = size;        // Else set to maximum possible
  51.   return this->number_elements;            // Return value
  52. }
  53.  
  54.  
  55. // void set_growth_ratio(float, char*) -- Set growth percentage of this stack
  56. // Input:                                 Float ratio and character string type
  57. // Output:                                None
  58.  
  59. void CoolBase_Stack::set_growth_ratio (float ratio, const char* Type) {
  60. #if ERROR_CHECKING
  61.   if (ratio <= 0.0) {                // If non-positive growth
  62.     //RAISE (Error, SYM(CoolStack), SYM(Negative_Ratio),
  63.     printf ("CoolStack<%s>::set_growth_ratio(): Negative growth ratio %f.\n",
  64.         Type, ratio);
  65.     abort ();
  66.   }
  67. #endif
  68.   this->growth_ratio = ratio;            // Adjust instance ration
  69.  
  70.  
  71. // void set_alloc_size(int, char*) -- Set the default allocation size 
  72. // Input:                             Integer size and character string type
  73. // Output:                            None
  74.  
  75. void CoolBase_Stack::set_alloc_size (int size, const char* Type) {
  76. #if ERROR_CHECKING
  77.   if (size < 0) {                    // If index out of range
  78.     //RAISE (Error, SYM(CoolStack), SYM(Negative_Size),
  79.     printf ("CoolStack<%s>::set_alloc_size(): Negative growth size %d.\n",
  80.         Type, size);
  81.     abort ();
  82.   }
  83. #endif
  84.   this->alloc_size_s = size;            // Set growth size
  85. }
  86.  
  87.  
  88. // CoolBase_Stack () -- Empty constructor for the CoolBase_Stack class
  89. // Input:            None
  90. // Output:           None
  91.  
  92. CoolBase_Stack::CoolBase_Stack () {
  93.   this->size = 0;                // Initialize size
  94.   this->number_elements = 0;            // Initialize element count
  95.   this->growth_ratio = 0.0;            // Intialize growth ratio
  96. }
  97.  
  98.  
  99. // CoolBase_Stack (long) -- constructor that specifies number of elements
  100. // Input:          Integer number of elements
  101. // Output:         None
  102.  
  103. CoolBase_Stack::CoolBase_Stack (long n) {
  104.   this->size = n;                // Element capacity
  105.   this->number_elements = 0;            // No elements
  106.   this->growth_ratio = 0.0;            // Initialize growth ratio 
  107. }
  108.  
  109.  
  110. // CoolBase_Stack (CoolBase_Stack&) -- constructor for reference to another stack
  111. // Input:                        CoolBase_Stack reference
  112. // Output:                       None
  113.  
  114. CoolBase_Stack::CoolBase_Stack (const CoolBase_Stack& s) {
  115.   this->growth_ratio = s.growth_ratio;        // New growth ratio
  116.   this->size = s.size;                // New size
  117.   this->number_elements = s.number_elements;    // New number of elements
  118. }
  119.  
  120.  
  121. // ~CoolBase_Stack -- Destructor for CoolBase_Stack class that frees up storage
  122. // Input:          None
  123. // Output:         None
  124.  
  125. CoolBase_Stack::~CoolBase_Stack () {
  126. }
  127.  
  128.  
  129. // CoolBase_Stack& operator= (CoolBase_Stack&) -- Assigns this stack to another stack
  130. // Input:                       Reference to a stack
  131. // Output:                      Reference to modified this
  132.  
  133. CoolBase_Stack& CoolBase_Stack::operator= (const CoolBase_Stack& s) {
  134.   this->number_elements = s.number_elements;    // New number of elements
  135.   this->growth_ratio = s.growth_ratio;          // New growth ratio
  136.   return *this;                    // Return reference
  137. }
  138.  
  139.  
  140. // assign_error -- Error message for parameterized CoolBase_Stack<Type>::operator=()
  141. // Input:          Character string of type
  142. // Output:         None
  143.  
  144. void CoolBase_Stack::assign_error (const char* Type) {
  145.   //RAISE (Error, SYM(CoolStack), SYM(Static_Size),
  146.   printf ("CoolStack<%s>::operator=(): Static-size stack.\n", Type);
  147.   abort ();
  148. }
  149.  
  150.  
  151. // top_error -- Error message for parameterized CoolStack<Type>::top()
  152. // Input:       Character string of type
  153. // Output:      None
  154.  
  155. void CoolBase_Stack::top_error (const char* Type) {
  156.   //RAISE (Error, SYM(CoolStack), SYM(No_Elements),
  157.   printf ("CoolStack<%s>::top(): No elements in stack.\n", Type);
  158.   abort ();
  159. }
  160.  
  161.  
  162. // pop_error -- Error message for parameterized CoolStack<Type>::pop()
  163. // Input:       Character string of type
  164. // Output:      None
  165.  
  166. void CoolBase_Stack::pop_error (const char* Type) {
  167.   //RAISE (Error, SYM(CoolStack), SYM(No_Elements),
  168.   printf ("CoolStack<%s>::pop(): No elements in stack.\n", Type);
  169.   abort ();
  170. }
  171.  
  172.  
  173. // bracket_error -- Error message for parameterized CoolStack<Type>::operator[]()
  174. // Input:           Character string of type and index
  175. // Output:          None
  176.  
  177. void CoolBase_Stack::bracket_error (const char* Type, long n) {
  178.   //RAISE (Error, SYM(CoolStack), SYM(Out_Of_Range),
  179.   printf ("CoolStack<%s>::operator[](): Index %d out of range.\n", Type, n);
  180.   abort ();
  181. }
  182.  
  183.  
  184. // push_error -- Error message for parameterized CoolStack<Type>::push()
  185. // Input:        Character string of type
  186. // Output:       None
  187.  
  188. void CoolBase_Stack::push_error (const char* Type) {
  189.   //RAISE (Error, SYM(CoolStack), SYM(Static_Size),
  190.   printf ("CoolStack<%s>::push(): Static-size stack.\n", Type);
  191.   abort ();
  192. }
  193.  
  194.  
  195. // popn_error -- Error message for parameterized CoolStack<Type>::popn()
  196. // Input:        Character string of type and index
  197. // Output:       None
  198.  
  199. void CoolBase_Stack::popn_error (const char* Type, long n) {
  200.   //RAISE (Error, SYM(CoolStack), SYM(Static_Size),
  201.   printf ("CoolStack<%s>::popn(): Negative stack index %d.\n", Type, n);
  202.   abort ();
  203. }
  204.  
  205.  
  206. // resize_error -- Error message for parameterized CoolStack<Type>::resize()
  207. // Input:          Character string of type and size
  208. // Output:         None
  209.  
  210. void CoolBase_Stack::resize_error (const char* Type, long new_size) {
  211.   //RAISE (Error, SYM(CoolStack), SYM(Negative_Size),
  212.   printf ("CoolStack<%s>::resize(): Negative resize %d.\n", Type, new_size);
  213.   abort ();
  214. }
  215.